package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.AlumniOrganize;
import org.tea.inventory.db.domain.AlumniOrganizeCustom;
import org.tea.inventory.db.domain.AlumniUser;
import org.tea.inventory.db.service.AlumniOrganizeService;
import org.tea.inventory.db.service.AlumniOrganizeUserService;
import org.tea.inventory.db.service.UserService;
import org.tea.inventory.db.util.wx.entity.MessageData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/organize")
public class OrganizeController {

    private static final Log logger = LogFactory.getLog(OrganizeController.class);


    @Autowired
    private AlumniOrganizeService alumniOrganizeService;

    @Autowired
    private AlumniOrganizeUserService alumniOrganizeUserService;

    @Autowired
    private UserService userService;


    /********************************************************* 通用 *********************************************************/


    /**
     * 根据类型查询组织
     *
     * @param orgType 组织类型
     * @return
     */
    @PostMapping("/selectOrganize")
    public Object selectOrganize(String orgType,String orgName,@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeService.selectOrganizeByTypeAndName(orgType,orgName,page,limit);

        return ResponseUtil.ok(map);
    }

    /**
     * 创建组织
     *
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("createOrganize")
    public Object createOrganize(@LoginAdmin Integer adminId, AlumniOrganizeCustom alumniOrganizeCustom){
    	
        alumniOrganizeCustom.setCreateDate(LocalDateTime.now());
        alumniOrganizeCustom.setOrganizeUserId(adminId);
        //后台创建默认审核通过
        alumniOrganizeCustom.setAuthStatus(1);

        Integer f = alumniOrganizeService.insertSelective(alumniOrganizeCustom);




        try {
            // 设置群主
            Integer ownerUserId = Integer.parseInt(alumniOrganizeCustom.getAdminUserId());
            alumniOrganizeService.OrganizeAdminUser(alumniOrganizeCustom.getId(),ownerUserId);
        } catch (Exception e) {

        }

        return ResponseUtil.ok(f);
    }

    /**
     * 更新组织
     *
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("updateOrganize")
    public Object updateOrganize(@LoginAdmin Integer adminId,AlumniOrganizeCustom alumniOrganizeCustom){
    	
        alumniOrganizeCustom.setModifyUserId(adminId);
        alumniOrganizeCustom.setModifyDate(LocalDateTime.now());

        Integer f = alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganizeCustom);


        // 设置群主
        // alumniOrganizeService.updateOrganizeAdminUser(alumniOrganizeCustom.getId(),Integer.parseInt(alumniOrganizeCustom.getAdminUserId()));

        try {
            Integer ownerUserId = Integer.parseInt(alumniOrganizeCustom.getAdminUserId());
            alumniOrganizeService.OrganizeAdminUser(alumniOrganizeCustom.getId(),ownerUserId);
        } catch (Exception e) {
        }


        return ResponseUtil.ok(f);
    }



    /**
     * 更新组织
     *
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("auditOrganizeInterest")
    public Object auditOrganizeInterest(@LoginAdmin Integer adminId,AlumniOrganizeCustom alumniOrganizeCustom){

        alumniOrganizeCustom.setModifyUserId(adminId);
        alumniOrganizeCustom.setModifyDate(LocalDateTime.now());
        alumniOrganizeCustom.setAuthDate(LocalDateTime.now());
        alumniOrganizeCustom.setAuthUserId(adminId);

        Integer f = alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganizeCustom);


        return ResponseUtil.ok(f);
    }

    /**
     * 删除组织
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("deleteOrgById")
    public Object deleteOrgById(@LoginAdmin Integer adminId, @RequestBody AlumniOrganizeCustom alumniOrganizeCustom){
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        alumniOrganizeService.deleteOrgById(alumniOrganizeCustom.getId());
        return ResponseUtil.ok();
    }

    /**
     * 更新组织审核状态
     * @param adminId
     * @param alumniOrganize
     * @return
     */
    @RequestMapping(value = "updateOrgAuth")
    public Object updateOrgAuth(@LoginAdmin Integer adminId, @RequestBody AlumniOrganize alumniOrganize){
        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganize);

        return ResponseUtil.ok();
    }

    @PostMapping("sendWXMsg")
    public Object sendWXMsg(Integer id){
        AlumniOrganize alumniOrganize = alumniOrganizeService.selectByPrimaryKey(id);

        // 发送微信消息
        AlumniUser u  = userService.findById(alumniOrganize.getOrganizeUserId());
        MessageData msgData = new MessageData();
        msgData.setKeyword1(u.getUserName());
        msgData.setKeyword2(alumniOrganize.getOrganizeName()+"兴趣小组申请");
        if ("1".equals(alumniOrganize.getAuthStatus().toString())){
            msgData.setKeyword3("通过");
            msgData.setKeyword4("您的兴趣小组"+ alumniOrganize.getOrganizeName()+"创建申请审核通过!");
        } else if ("2".equals(alumniOrganize.getAuthStatus().toString())){
            msgData.setKeyword3("驳回");
            msgData.setKeyword4("您的兴趣小组"+ alumniOrganize.getOrganizeName()+"创建申请被驳回!");
        }
        userService.sendWXMSG(alumniOrganize.getOrganizeUserId(),msgData);

        return ResponseUtil.ok();
    }

    /**
     * 批量删除圈子
     * @param adminId
     * @param orgIds
     * @return
     */
    @PostMapping("deleteOrgQuantity")
    public Object deleteOrgQuantity(@LoginAdmin Integer adminId,String orgIds){
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        if (orgIds == null) {
            return ResponseUtil.badArgument();
        }
        alumniOrganizeService.deleteOrgQuantity(Arrays.asList(orgIds.split(",")));
        return ResponseUtil.ok();
    }


    @PostMapping("selectUserByUserName")
    public Object selectUserByUserName(@LoginAdmin Integer adminId,String userName){

        List<AlumniUser> list = userService.selectUserByUserName(userName);

        return ResponseUtil.ok(list);
    }





    /********************************************************* 地方专委会 *********************************************************/


    /********************************************************* 行业专委会 *********************************************************/


    /********************************************************* 兴趣小组 *********************************************************/
}
